<?php
/**
 * User:chendenan
 * Created On 2022/3/10 0010 22:51
 */

namespace app\common\lib\sms;

use think\facade\Log;

class DxwSms
{

    public $accesskey = null;
    public $secret = null;
    public function __construct($accesskey = "", $secret = "")
    {

        $this->accesskey = config("dxw.accesskey");
        $this->secret = config("dxw.secret");
        if (empty($this->accesskey)) {
            $this->accesskey = $accesskey;
        }
        if (empty($this->secret)) {
            $this->secret = $secret;
        }
    }

    public function warp_query($params)
    {
        $params['accesskey'] = $this->accesskey;
        $params['secret'] = $this->secret;
        return $params;
    }

    private function send_request($url, $params = '')
    {
        $headers = array(
            "Content-Type:application/x-www-form-urlencoded",
        );

        if (is_array($params)) {
            $params = http_build_query($params, null, '&');
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        $result = curl_exec($ch);
        curl_close($ch);
        $result = json_decode($result); //返回JSON状态消息
        Log::info("dxwsms-send-result-".$result->code."-".$result->msg."-".$params);
        if($result->code>0){
            return false;
        }else{
            return true;
        }
        //return $result;
    }

    public function sendCode($phone,$code){
        return $this->send_verify_code($phone,$code.'##1',"【创瑞短信】", '85992');
    }

    #发送验证码接口
    public function send_verify_code($mobile, $content, $sign, $templateId)
    {
        $api_url = config("dxw.host")."/api/v2/single_send";
        $params = $this->warp_query([
            'mobile' => $mobile,
            'content' => $content,
            'sign' => $sign,
            'templateId' => $templateId]);
        return $this->send_request($api_url, $params);
    }

    #群发短信接口
    public function send_sms($sign, $mobile = "", $content = "", $data = "", $templateId = "", $scheduleSendTime = "")
    {

        $api_url = config("dxw.host")."/api/v2/send";
        $params = $this->warp_query([
            'mobile' => $mobile,
            'content' => $content,
            'templateId' => $templateId,
            'scheduleSendTime' => $scheduleSendTime,
            'sign' => $sign,
            'data' => $data]);
        return $this->send_request($api_url, $params);
    }

    public function send_i18n_sms($sign, $mobile = "", $content = "", $data = "", $templateId = "", $scheduleSendTime = "")
    {
        $api_url = config("dxw.host")."/intl/api/v2/send";
        $params = $this->warp_query([
            'mobile' => $mobile,
            'content' => $content,
            'templateId' => $templateId,
            'scheduleSendTime' => $scheduleSendTime,
            'sign' => $sign,
            'data' => $data]);
        return $this->send_request($api_url, $params);
    }

    /**
     * 主动获取回复列表
     * @return mixed
     */
    public function get_replay_list()
    {
        $api_url = config("dxw.host")."/report/up";
        $params = $this->warp_query([]);
        return $this->send_request($api_url, $params);
    }

    #主动获取短信状态
    public function get_send_status()
    {
        $api_url = config("dxw.host")."/report/status";
        $params = $this->warp_query([]);
        return $this->send_request($api_url, $params);
    }

    #短信签名查询接口
    public function get_signature_list()
    {
        $api_url = config("dxw.host")."/query/signlist";
        $params = $this->warp_query([]);
        return $this->send_request($api_url, $params);
    }

    #添加签名接口
    public function create_sms_signature($sign, $description = "")
    {
        $api_url = config("dxw.host")."/open/api/addSign";
        $params = $this->warp_query(["sign" => $sign, "description" => $description]);
        return $this->send_request($api_url, $params);
    }

    #修改签名
    public function update_sms_signature($id, $sign, $description = "")
    {
        $api_url = config("dxw.host")."/open/api/modifySign";
        $params = $this->warp_query([
            "id" => $id,
            "sign" => $sign,
            "description" => $description]);
        return $this->send_request($api_url, $params);
    }

    #获取模板列表
    public function get_template_list()
    {
        $api_url = config("dxw.host")."/query/templatelist";
        $params = $this->warp_query([]);
        return $this->send_request($api_url, $params);
    }


    /**
     * 添加模板
     * @param template_name $
     * @param category_id $
     * @param template $
     * @param description $
     * @return mixed
     */
    public function create_template($templateName, $categoryId, $template, $description)
    {
        $api_url = config("dxw.host")."/open/api/addTemplate";
        $params = [
            "templateName" => $templateName,
            "categoryId" => $categoryId,
            "template" => $template,
            "description" => $description
        ];
        return $this->send_request($api_url, $params);
    }

    /**
     * 修改模板
     * @param id $
     * @param template_name $
     * @param template $
     * @param description $
     * @return mixed
     */
    public function update_template($id, $templateName, $template, $description = "")
    {

        $api_url = config("dxw.host")."/open/api/addTemplate";
        $params = $this->warp_query([
            "id" => $id,
            "templateName" => $templateName,
            "template" => $template,
            "description" => $description
        ]);
        return $this->send_request($api_url, $params);
    }

    /**
     * 短信余额查询
     * @return mixed
     */
    public function account()
    {
        $api_url = config("dxw.host")."/query/account";
        $params = $this->warp_query([]);
        return $this->send_request($api_url, $params);
    }

    /**
     * 查询手机号所在地区
     * @param $mobiles
     * @return mixed
     */
    public function query_number_region($mobiles)
    {
        $api_url = config("dxw.host")."/open/api/qry/mobiles/section";
        $params = $this->warp_query(["mobiles" => $mobiles]);
        return $this->send_request($api_url, $params);
    }

    /**
     * 发送语音验证码
     * @param $mobile
     * @param $voiceCode
     * @return mixed
     */
    public function send_voice_verfiy_code($mobile, $voiceCode)
    {
        $api_url = config("dxw.host")."/voice/send";
        $params = $this->warp_query(["mobile" => $mobile, 'voiceCode' => $voiceCode]);
        return $this->send_request($api_url, $params);
    }

    /**
     * 发送彩信
     * @param $title
     * @param $content
     * @param $mobile
     * @param string $scheduleSendTime
     * @return mixed
     */
    public function send_mms($title, $content, $mobile, $scheduleSendTime = "")
    {
        $api_url = config("dxw.host")."/mms/api/send";
        $params = $this->warp_query(['title' => $title,
            'content' => $content,
            'mobile' => $mobile,
            'scheduleSendTime' => $scheduleSendTime]);

        return $this->send_request($api_url, $params);
    }

    public function send_voice_code($mobile, $voiceCode)
    {
        $api_url = config("dxw.host")."/voice/send";
        $params = $this->warp_query([
            'mobile' => $mobile,
            'voiceCode' => $voiceCode
        ]);

        return $this->send_request($api_url, $params);
    }

}

// $sp = new OneCloudSPService();
// $ret = $sp->send_verify_code('18912345678', '1234##5', "【创瑞短信】", '178855');
//$ret = $sp->get_replay_list();
//$ret = $sp->get_send_status();
//$ret = $sp->account();
//$ret = $sp->get_signature_list();
//$ret = $sp->get_template_list();
//$ret = $sp->query_number_region("18912345678,18959619021");
//$ret = $sp->send_voice_verfiy_code("18912345678", "1234");
// $ret = $sp->send_mms("测试PHPSDK", "3,测试", "18912345678", "");
// print_r($ret);



